<template>
	<el-dialog title="报警批量处理" v-model="visible" :close-on-click-modal="false" draggable width="500">
		<el-form ref="dataFormRef" :model="form" :rules="dataRules" formDialogRef label-width="90px" v-loading="loading">
			<el-row :gutter="24">
				<el-col>
					<el-form-item label="处理类型" prop="processTypeId">
						<el-radio-group v-model="form.processTypeId">
							<el-radio v-for="item in processTypeOptions" :key="item.id" :label="item.id">
								<span :style="{ color: getProcess(item.processTypeLevel)?.value }">{{ item.processTypeName }}</span>
							</el-radio>
						</el-radio-group>
					</el-form-item>
				</el-col>
			</el-row>
		</el-form>
		<template #footer>
			<span class="dialog-footer">
				<el-button @click="visible = false">取消</el-button>
				<el-button type="primary" @click="onSubmit" :disabled="loading">确认</el-button>
			</span>
		</template>
	</el-dialog>
</template>

<script setup lang="ts" name="AlarmHandlingForm">
import { useMessage } from '/@/hooks/message';
import { batchHandle } from '/@/api/alarm/alarmLogging';
import { processTypeLevelOptions } from '../../alarmProcessType/data';
import { fetchListAll as fetchProcessTypeListAll } from '/@/api/alarm/alarmProcessType';
const emit = defineEmits(['refresh']);

// 定义变量内容
const dataFormRef = ref();
const visible = ref(false);
const loading = ref(false);
const processTypeOptions = ref<any[]>([]);
// 定义字典

// 提交表单数据
const form = reactive({
	ids: [] as string[],
	remarks: '批量处理',
	processTypeId: '',
	processTypeName: '',
	processTypeLevel: '',
});

// 定义校验规则
const dataRules = ref({
	processTypeId: [{ required: true, message: '请选择处理类型', trigger: 'change' }],
});

// 打开弹窗
const openDialog = (ids: string[]) => {
	visible.value = true;
	nextTick(() => {
		dataFormRef.value?.resetFields();
	});
	form.ids = ids;
	// 重置表单数据
	getProcessTypes();
};

// 提交
const onSubmit = async () => {
	const valid = await dataFormRef.value.validate().catch(() => {});
	if (!valid) return false;

	try {
		loading.value = true;
		const process = processTypeOptions.value.find((d) => d.id === form.processTypeId);
		form.processTypeName = process.processTypeName;
		form.processTypeLevel = process.processTypeLevel;
		await batchHandle(form);
		useMessage().success('操作成功！');
		visible.value = false;
		emit('refresh');
	} catch (err: any) {
		useMessage().error(err.msg);
	} finally {
		loading.value = false;
	}
};

const getProcessTypes = () => {
	fetchProcessTypeListAll({ state: 20 }).then((res) => {
		processTypeOptions.value = res.data;
	});
};
// 查询处理类型的颜色
const getProcess = (processTypeLevel: number) => {
	const item = processTypeLevelOptions.find((d) => d.id === processTypeLevel);
	return item;
};
// 暴露变量
defineExpose({
	openDialog,
});
</script>
